//
//  ChangePhoneNumController.m
//  tryshopping.boss
//
//  Created by Mr.Lee on 16/12/6.
//  Copyright © 2016年 Reabam. All rights reserved.
//

#import "ChangePhoneNumController.h"
#import "BindPhoneViewController.h"
#import "IQKeyboardManager.h"

@interface ChangePhoneNumController ()


// 验证码输入框
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;

// 下一步按钮
@property (weak, nonatomic) IBOutlet UIButton *bindPhoneBtn;

// 获取验证码
@property (weak, nonatomic) IBOutlet UIButton *verificationCode;

// 旧手机号码
@property (weak, nonatomic) IBOutlet UILabel *oldPhoneNum;



@end

@implementation ChangePhoneNumController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"改绑手机号";
    self.view.backgroundColor = zuiqianhuiColor;
    // 设置导航条文字的大小及颜色
    self.navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:17]};
    // 设置下一步按钮
    self.bindPhoneBtn.layer.cornerRadius = 4.0;
    self.bindPhoneBtn.layer.masksToBounds = YES;
    
    // 设置获取验证码按钮
    self.verificationCode.layer.cornerRadius = 4.0;
    self.verificationCode.layer.masksToBounds = YES;
    
    // 设置旧手机号
    self.oldPhoneNum.text = self.purPhoneNum;
    
    // 是否输入验证码的通知
    [self.phoneTF addTarget:self action:@selector(phoneTFChanged:) forControlEvents:UIControlEventEditingChanged];
    
    
}
- (void)phoneTFChanged:(UITextField *)tf{
    if (tf.text.length >0) {
        [self.bindPhoneBtn setBackgroundColor:CommonColor];
        self.bindPhoneBtn.enabled = YES;
    } else{
        [self.bindPhoneBtn setBackgroundColor:[UIColor colorWithHexString:@"cccccc"]];
        self.bindPhoneBtn.enabled = NO;
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];

}

// 点击下一步
- (IBAction)nextStepBtnClick:(UIButton *)sender {
    [[IQKeyboardManager sharedManager] resignFirstResponder];

//    BindPhoneViewController *bindPhone = [[BindPhoneViewController alloc] init];
//    bindPhone.view.backgroundColor = [UIColor whiteColor];
//    [self.navigationController pushViewController:bindPhone animated:YES];
    
    
    
    if ([_phoneTF.text length]<1)
    {
        [TSAlertTool showAlertMessage:@"请输入手机号码"];
    } else {
        // 验证原手机
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* tokenId = [user objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:tokenId forKey:@"tokenId"];

        NSString *userCode = [user objectForKey:@"userCode"];
        [params setValue:userCode forKey:@"ucode"];      // 原手机号码
        [params setValue:self.phoneTF.text forKey:@"mcode"];  // 短信验证码

//        DLog(@"====%@",params);

        [HttpRequest requestData1:@"Common/ValidPhone" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                NSLog(@"====%@",severDataDict.dataDict);

                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [MBProgressHUD hideProgressHUDInternal:self.view];

                    // 验证成功之后去绑定新手机界面
                    BindPhoneViewController *bindPhone = [[BindPhoneViewController alloc] init];
                    bindPhone.vcodeStr = [severDataDict.dataDict objectForKey:@"vcode"];
                    [self.navigationController pushViewController:bindPhone animated:YES];

                }else{
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                }
            }
            else
            {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:subStr view:self.view];
            }
        }];
    }
    
}


// 获取验证码
- (IBAction)VerificationCodeBtn:(UIButton *)sender {
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [sender setTitle:@"获取验证码" forState:UIControlStateNormal];
                sender.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //                NSLog(@"____%@",strTime);
                if (strTime.intValue==59) {
                    NSLog(@"____%@",strTime);
                    NSMutableDictionary *params = [NSMutableDictionary new];
                    [params setValue:self.purPhoneNum forKey:@"mobile"];
                    [params setValue:@"R" forKey:@"msgType"];
                    
//                    DLog(@"params ==  %@", params);
                    [HttpRequest requestData1:@"Common/SendMsg" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                        if (isSuccess) {
                            NSLog(@"====%@",severDataDict.dataDict);
                            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                                //                                        [MBProgressHUD hideProgressHUDInternal:self];
                                //                                        NSString*ResultString=[severDataDict.dataDict objectForKey:@"ResultString"];
                                //                                        [self showAlertMessage:ResultString];
                                [MBProgressHUD showTextHudSub:@"已成功获取验证码" view:self.view];
                                
                                
                            }else{
//                                [MBProgressHUD hideProgressHUDInternal:self.view];
                                [MBProgressHUD showTextHudSub:@"获取验证码失败" view:self.view];
                            }}
                        else
                        {
                            [MBProgressHUD hideProgressHUDInternal:self.view];
                            NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
                            [MBProgressHUD showTextHudSub:subStr view:self.view];
                        }
                    }];
                }
                
                if (![strTime isEqualToString:@"00"]) {
                    [sender setTitle:[NSString stringWithFormat:@"%@秒重获",strTime] forState:UIControlStateNormal];
                }
                sender.userInteractionEnabled = NO;
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
}

@end
